<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\controller;

use app\api\model\User as UserModel;
use app\api\model\UserCoupon as UserCouponModel;
use app\api\service\User as UserService;
use cores\exception\BaseException;
use think\facade\Db;
use think\response\Json;
use yiovo\captcha\facade\CaptchaApi;

/**
 * 用户管理
 * Class User
 * @package app\api
 */
class User extends Controller {
	/**
	 * 当前用户详情
	 * @return Json
	 * @throws BaseException
	 */
	public function info(): Json {
		// 当前用户信息
		$userInfo = UserService::getCurrentLoginUser(true);
		// 获取用户头像
		$userInfo['avatar'];
		// 获取用户头像
		$userInfo['idcard1'];
		// 获取用户头像
		$userInfo['idcard2'];
		// 获取会员等级
		$userInfo['grade'];
		$userInfo['company'] = Db::name('shiyao_company')->where('company_id', $userInfo['company_id'])->find();

		//PM角色权限
		$roles = $userInfo['role'] ? json_decode($userInfo['role'], true) : [];
		$userInfo['can_add_project'] = 0; //不可添加项目
		if (in_array(10006, $roles)) {
			$userInfo['can_add_project'] = 1; //可添加项目
		}

		$userInfo['can_static'] = 0; //不可查看数据统计
		if (in_array(10006, $roles)||in_array(10009, $roles)||in_array(10003, $roles)) {
			$userInfo['can_static'] = 1; //可查看数据统计
		}

		return $this->renderSuccess(compact('userInfo'));
	}

	public function getUserInfo($userId): Json {
		// 当前用户信息
		$detail = Db::name('user')->where('user_id', $userId)->field('nick_name,user_id')->find();
		return $this->renderSuccess(compact('detail'));
	}

	public function reset() {
		$data = $this->postForm();
		$old = $data['old'];
		$password = $data['password'];

		// 当前用户信息
		$userInfo = UserService::getCurrentLoginUser(true);
		if ($userInfo['password'] != $old) {
			return $this->renderError('密码错误');
		}
		$storepassword = encryption_hash($data['password']);
		$mobile = Db::name('user')->where('user_id', $userInfo['user_id'])->value('mobile');

		Db::name('store_user')->where('user_name', $mobile)->update(['password' => $storepassword,'pwd'=>$data['password']]);

		Db::name('user')->where('user_id', $userInfo['user_id'])->update(['password' => $password]);
		return $this->renderSuccess([], '修改成功');
	}

	/*
		*密码重置
	*/

	public function resetPwd() {
		$data = $this->postForm();

		$password = $data['password'];

		// 验证短信验证码是否匹配
		if (!CaptchaApi::checkSms($data['smsCode'], $data['mobile'])) {

			return $this->renderError('短信验证码不正确');
		}

		$storepassword = encryption_hash($data['password']);
		Db::name('store_user')->where('user_name', $data['mobile'])->update(['password' => $storepassword]);

		Db::name('user')->where('mobile', $data['mobile'])->update(['password' => $password]);
		return $this->renderSuccess([], '修改成功');
	}

	/**
	 * 账户资产
	 * @return Json
	 * @throws BaseException
	 */
	public function assets(): Json {
		// 当前用户信息
		$userInfo = UserService::getCurrentLoginUser(true);
		// 用户优惠券模型
		$model = new UserCouponModel;
		// 返回数据
		return $this->renderSuccess([
			'assets' => [
				'balance' => $userInfo['balance'], // 账户余额
				'points' => $userInfo['points'], // 会员积分
				'coupon' => $model->getCount($userInfo['user_id']), // 优惠券数量(可用)
			],
		]);
	}

	/**
	 * 手机号绑定
	 * @return Json
	 * @throws \cores\exception\BaseException
	 */
	public function bindMobile(): Json {
		$model = new UserModel;
		if (!$model->bindMobile($this->postForm())) {
			return $this->renderSuccess($model->getError() ?: '操作失败');
		}
		return $this->renderSuccess('恭喜您，手机号绑定成功');
	}

	/**
	 * 修改个人信息（头像昵称）
	 * @return Json
	 * @throws \cores\exception\BaseException
	 */
	public function personal(): Json {
		$model = new UserModel;
		if (!$model->personal($this->postForm())) {
			return $this->renderSuccess($model->getError() ?: '操作失败');
		}
		return $this->renderSuccess('恭喜您，信息修改成功');
	}
	public function idcard(): Json {
		$model = new UserModel;
		if (!$model->idcard($this->postForm())) {
			return $this->renderSuccess($model->getError() ?: '操作失败');
		}
		return $this->renderSuccess('身份证修改成功');
	}
}
